package com.zkingsoft.actions.developer;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.model.Btn;
import com.zkingsoft.model.Function;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.BtnService;
import com.zkingsoft.services.FunctionService;

/**
 * @description 功能管理action
 * @author 姜友瑶
 * @email 935090232@qq.com
 * @date 2016-06-26
 */
@Controller
@RequestMapping(value = "/developer/function")
public class FunctionController extends BaseController {

	@Resource
	private FunctionService currentService;

	@Resource
	private BtnService btnService;

	/**
	 * 
	 * @Description: 功能分页查询
	 * @author:姜友瑶
	 * @param request
	 * @param function
	 * @param pageVo
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, Function function, PaginationVO pageVo) {
		return showList(currentService, function, pageVo);
	}

	/**
	 * 
	 * @Description: 功能的新增和修改
	 * @author:姜友瑶
	 * @param function
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Function function) {
		// 设置功能等级
		if (function.getParentId() == -10) {
			function.setGrade(1);
		} else {
			if(function.getParentId()!=null){
				Function parent = currentService.findById(function.getParentId());
				function.setGrade(parent.getGrade() + 1);
			}else{
				function.setGrade(1);
			}
		}
		if (function.getId() != null) {
			if (function.getBtns() == null || function.getBtns().equals("")) {
				function.setBtns("");
			}
			return modify(currentService, function, "功能");
		} else {
			// 设置默认启用
			function.setIsDisable(SystemConstance.IS_N);
			return add(currentService, function, "功能");
		}
	}

	/**
	 * 
	 * @Description: 功能的启用和禁用
	 * @author:姜友瑶
	 * @param function
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/startOrStopFunction")
	public @ResponseBody AjaxResult startOrStopFunction(Function function) {
		return modify(currentService, function, "功能");
	}

	/**
	 * 
	 * @Description: 删除功能
	 * @author:姜友瑶
	 * @param keys
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	/**
	 * 
	 * @Description: 进入功能编辑界面
	 * @author:姜友瑶
	 * @param id
	 * @param request
	 * @return 返回类型 String
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/editForm")
	public String editForm(Long id, HttpServletRequest request) {
		Function function;
		if (id != null) {
			function = currentService.findById(id);
			request.setAttribute("obj", function);
		}
		List<Btn> btnList = btnService.findByModel(null);
		request.setAttribute("btnList", btnList);
		return "developer/function-form";
	}

	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		return findByModel(currentService, null);
	}

	/**
	 * 
	 * @Description: 获取顶层功能
	 * @author:姜友瑶
	 * @param function
	 * @param request
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/topFunction")
	public @ResponseBody AjaxResult topFunction(Function function, HttpServletRequest request) {
		function.setGrade(1);
		return new AjaxResult(AjaxResult.STATUS_OK, null, "执行成功", currentService.findByModel(function), 0);

	}

	/**
	 * 根据对象查询
	 * 
	 * @param function
	 * @return
	 */
	@RequestMapping(value = "/findByModel")
	public @ResponseBody AjaxResult findByModel(Function function, HttpServletRequest request) {
		return new AjaxResult(AjaxResult.STATUS_OK, null, "执行成功", currentService.findByModel(function), 0);
	}

	/**
	 * 
	 * @Description: 根据id查询功能
	 * @author:姜友瑶
	 * @param id
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/findById")
	public @ResponseBody AjaxResult findById(Long id) {
		Function function = super.findById(currentService, id);
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, Arrays.asList(function), 0);
	}

}